#!/data/data/com.termux/files/usr/bin/bash
autoCheck() {
    cur=$(pwd)
    if [ "$(uname -o)" = "Android" ]; then
        ANDROIDTERMUX
    else
        GNULINUX
    fi
}
####################################################
GNULINUX() {
    dependencies=""

    if [ ! -e /usr/bin/xz ]; then
        dependencies="${dependencies} xz-utils"
    fi

    if [ ! -e /usr/bin/git ]; then
        dependencies="${dependencies} git"
    fi

    if [ ! -e /usr/bin/whiptail ]; then
        dependencies="${dependencies} whiptail"
    fi

    if [ ! -z "$dependencies" ]; then
        echo "正在安装相关依赖..."
        if grep -Eqii "Alpine" /etc/issue || grep -Eq "Alpine" /etc/*-release; then

            apk add -q newt tar git xz
        elif grep -Eqi "Arch" /etc/issue || grep -Eqi "Manjaro" /etc/issue; then

            pacman -Sy --noconfirm ${dependencies}

        elif grep -Eqi "Fedora" /etc/issue || grep -Eqii "CentOS" /etc/issue || grep -Eqi "Red Hat Enterprise Linux Server" /etc/issue; then

            dnf install -y ${dependencies} || yum install -y ${dependencies}
        else
            apt update
            apt install -y ${dependencies} || port install ${dependencies} || zypper in ${dependencies} || emerge ${dependencies} || guix package -i ${dependencies} || pkg install ${dependencies} || pkg_add ${dependencies} || pkgutil -i ${dependencies} || opkg install -y ${dependencies}

        fi

    fi
    PREFIX="/usr"
    mkdir -p /usr/tmp
    BUILDORDOWNLOAD

}

##############################################
ANDROIDTERMUX() {
    dependencies=""

    if [ ! -e $PREFIX/bin/whiptail ]; then
        dependencies="${dependencies} dialog"
    fi

    if [ ! -e $PREFIX/bin/git ]; then
        dependencies="${dependencies} git"
    fi

    if [ ! -z "$dependencies" ]; then
        echo "正在安装相关依赖..."
        apt update
        apt install -y ${dependencies}
    fi

    BUILDORDOWNLOAD
}
################################
BUILDORDOWNLOAD() {
    if (whiptail --title "How do you want to install annie?" --yes-button 'Download下载' --no-button 'Build编译' --yesno "下载的版本仅支持安卓，可能不会及时更新，但更节省时间。手动编译的是新版，需要安装golang，占用空间比较大。\nThe statically compiled version may not be updated in time, but it saves time. Manually compiled is the latest version, which needs to install golang, which takes up a lot of space." 12 60); then
        DOWNLOADANNIE
    else
        GOBUILDANNIE
    fi
}
##################################
GOBUILDANNIE() {
    if [ ! -e $PREFIX/bin/go ]; then
        apt update
        apt install -y golang
    fi

    cd $PREFIX/tmp
    if [ ! -d "ANNIETMPFILE/.git" ]; then
        rm -rf ANNIETMPFILE
    fi

    if [ -d "ANNIETMPFILE" ]; then
        cd ANNIETMPFILE
        git reset --hard
        git pull
    else
        git clone --depth=1 git://github.com/iawia002/annie.git ANNIETMPFILE
        cd ANNIETMPFILE
    fi

    go build -o $PREFIX/bin/annie
    cd ${cur}
    annie
    annie -v
}
#############################################
DOWNLOADANNIE() {
    cd $PREFIX/tmp

    if [ -d ".ANNIEAndroidaarch64" ]; then
        rm -rf .ANNIEAndroidaarch64
    fi

    git clone -b android --depth=1 https://gitee.com/mo2/annie.git .ANNIEAndroidaarch64
    cd .ANNIEAndroidaarch64

    tar -Jxvf annie.tar.xz -C $PREFIX/bin/
    cd ${cur}
    rm -rf $PREFIX/tmp/.ANNIEAndroidaarch64
    annie
    annie -v
}
#####################################
autoCheck
